package com.levi.juc.singlemodel;

/**
 * @author shentong
 * @since 2022/6/13 10:38 PM
 */
public class SecuritysingleModel {

    private SecuritysingleModel(){}

    private static volatile SecuritysingleModel securitysingleModel;

    public static SecuritysingleModel getInstance() {
        if (securitysingleModel == null){
            synchronized (SecuritysingleModel.class){
                if (securitysingleModel == null) {
                    securitysingleModel = new SecuritysingleModel();
                }
            }
        }
        return securitysingleModel;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 500; i++) {
            new Thread(()->{
                SecuritysingleModel instance = getInstance();
                System.out.println(Thread.currentThread().getName()+": =>>" + instance);
            }).start();
        }
    }


    static class Singleton{
        private static volatile Singleton singleton;

        private Singleton() {

        }


        public static Singleton getSingleton() {
            if (singleton == null) {
                synchronized (Singleton.class){
                    if (singleton == null) {
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }

        public static void main(String[] args) {
            for (int i = 0; i < 200; i++) {
                new Thread(()->{
                    Singleton singleton = getSingleton();
                    System.out.println(Thread.currentThread().getName()+"<><><>"+singleton);
                }).start();
            }
        }
    }
}
